(function () {
    let id;
    let answer;

    const hides = document.querySelectorAll(".hide");
    // 获取密保
    // 第一个按钮
    const oneBtn = document.querySelector("#one");
    const phoneDom = document.querySelector("#phone");
    axios.defaults.baseURL = 'http://124.223.14.236:3001/api'
    oneBtn.addEventListener("click", async function () {
        const phoneValue = phoneDom.value.trim();
        if (!phoneValue) return alert("请输入手机号");
        const { data } = await axios.get('/user/findPwd', {
            params: {
                phone: phoneValue,
            },
        })
        if (!data.success) return alert(data.msg);
        // 成功后跳到下一个,并赋值上去
        hides[0].classList.remove("activeNow");
        hides[1].classList.add("activeNow");
        const inputs = document.querySelectorAll(".activeNow:nth-child(2) input");
        inputs[0].value = data.data.username;
        inputs[1].value = data.data.phone;
        inputs[2].value = data.data.question;
        // 获取id
        id = data.data.id;
    });

    // 第二个按钮
    const twoBtn = document.querySelector("#two");
    twoBtn.addEventListener("click", function () {
        // 获取填写的密保答案
        answer = hides[1]
            .querySelector(".box .input-item:nth-child(4) input")
            .value.trim();
        // 成功后跳到下一个
        hides[1].classList.remove("activeNow");
        hides[2].classList.add("activeNow");
    });

    // 第三个按钮
    const threeBtn = document.querySelector("#three");
    threeBtn.addEventListener("click", async function () {
        const password = hides[2].querySelector(".box input").value.trim();
        const res = await axios.post('/user/resetPwd', {
            id: `${id}`,
            password,
            answer,
        })
        if (!res.data.success) return alert(res.data.msg);
        alert("修改密码成功！");
        window.location.href = "/01-loginBaseCode/login.html";
    });

    // 回到上一个
    twoBtn.previousElementSibling.addEventListener("click", function () {
        hides[1].classList.remove("activeNow");
        hides[0].classList.add("activeNow");
    });

    threeBtn.previousElementSibling.addEventListener("click", function () {
        hides[2].classList.remove("activeNow");
        hides[1].classList.add("activeNow");
    });
})();